package com.webex.ta.hydra.gui.action;

import com.webex.ta.hydra.Constants;
import com.webex.ta.hydra.core.TestPlan;
import com.webex.ta.hydra.gui.GuiPackage;
import com.webex.ta.hydra.gui.project.HydraProjectSuite;
import com.webex.ta.hydra.gui.util.FileDialoger;
import com.webex.ta.hydra.save.SaveService;
import org.apache.commons.io.FilenameUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.*;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-8-30
 * Time: 10:57:41
 */
public class SaveAction implements ActionHandler {

    private static Set<String> commands = new HashSet<String>();
	static {
        commands.add(ActionNames.SAVE);
        commands.add(ActionNames.SAVE_AS);
	}

    public void doAction(ActionEvent e) {
        GuiPackage gp = GuiPackage.getInstance();
        HydraProjectSuite project = gp.getActiveProject();
        if (project == null)
            return;
        
        File updateFile = project.getFile();
        String updateFilePath;
		if (!ActionNames.SAVE.equals(e.getActionCommand()) || updateFile == null) {
            // Save as or save a new project
			JFileChooser chooser = FileDialoger.promptToSaveFile("" + Constants.HYDRA_FILE_EXTENSION);
			if (chooser == null) {
				return;
			}
			updateFilePath = chooser.getSelectedFile().getAbsolutePath();
			// Make sure the file ends with proper extension
			if(FilenameUtils.getExtension(updateFilePath).equals("")) {
				updateFilePath = updateFilePath + Constants.HYDRA_FILE_EXTENSION;
			}
			// Check if the user is trying to save to an existing file
			if(!e.getActionCommand().equals(ActionNames.SAVE)) {//so it must be a SAVE_AS action
				File f = new File(updateFilePath);
				if(f.exists()) {
					int response = JOptionPane.showConfirmDialog(gp.getIdeFrame(),
							"The selected file already exists, do you want to overwrite it?",
							"Save?",
							JOptionPane.YES_NO_OPTION,
							JOptionPane.QUESTION_MESSAGE);
					if (response == JOptionPane.CLOSED_OPTION || response == JOptionPane.NO_OPTION) {
						return ; // Do not save, user does not want to overwrite
					}
				}
			}

		    gp.setTestPlanFile(updateFilePath);
            gp.getIdeFrame().projectFileLoaded(updateFilePath);
		} else {
            updateFilePath = updateFile.getAbsolutePath();
        }
        

        TestPlan tp = gp.getHydraTable().getModel().getTestPlan();
        File f = new File(updateFilePath);

        try {
            OutputStream ostream = new FileOutputStream(f);
            SaveService.saveTestPlan(tp, ostream);
            project.setFile(f);
            gp.getIdeFrame().updateProjectName(project);
            project.takeSnapshot();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public Set<String> getActionNames() {
        return commands;
    }
}
